import {
  getStudentListByCondition,
  reqGetDepAndMajor,
  reqBatchAddStudent,
  reqEditStudent,
  reqDelStudent,
  reqCheckStudentIdIsExisting,
  reqGetStudentInfoById,
} from '@/views/Custom/StudentMng/api/studentApi';
import Cookies from 'js-cookie';
const state = {
  studentIPage: {}, // 学生分页信息对象
  studentList: [],
  depAndMajorMenu: [], // 院系与专业的树形结构数据
  studentInfo: {}, // 学生信息
};

const mutations = {
  GETSTUDENTINFOBYSTUDENTID(state, result) {
    state.studentInfo = result;
  },
  GETSTUDENTLISTBYCONDITION(state, result) {
    return (state.studentIPage = result);
  },
  GETDEPANDMAJORANDCLASSROOMMENU(state, result) {
    return (state.depAndMajorMenu = result);
  },
};

const actions = {
  // 根据学号查询学生信息
  async getStudentInfoByStudentId({ commit }, params) {
    let result = await reqGetStudentInfoById(params);
    commit(
      'GETSTUDENTINFOBYSTUDENTID',
      result.data ? result.data.studentInfo : {}
    );
  },

  // 分页条件查询学生信息
  async getStudentListByCondition({ commit }, params) {
    let result = await getStudentListByCondition(params);
    commit('GETSTUDENTLISTBYCONDITION', result.data.studentIPage);
  },
  // 获取院系与专业、班级的树形结构数据
  async getDepAndMaJorAndClassroomMenu({ commit }, params) {
    if (!Cookies.get('depAndMajorMenu')) {
      let result = await reqGetDepAndMajor(params);
      commit('GETDEPANDMAJORANDCLASSROOMMENU', result.data.depAndMajorMenu);
      Cookies.set(
        'depAndMajorMenu',
        JSON.stringify(result.data.depAndMajorMenu),
        { expires: 1 }
      );
    } else {
      commit(
        'GETDEPANDMAJORANDCLASSROOMMENU',
        JSON.parse(Cookies.get('depAndMajorMenu'))
      );
    }
  },
  async batchAddStudent({ commit }, params) {
    let result = await reqBatchAddStudent(params);
    return result;
  },

  // 学生编辑
  async editStudent({ commit }, params) {
    let result = await reqEditStudent(params);
    return result;
  },

  // 学生删除
  async batchDelStudent({ commit }, params) {
    let result = await reqDelStudent(params);
    return result;
  },
  // 根据学号查询学生信息,查看学号是否存在
  async checkStudentIdIsExisting({ commit }, params) {
    let result = await reqCheckStudentIdIsExisting(params);
    return result.data;
  },
};
const getters = {
  studentInfo(state) {
    return state.studentInfo || {};
  },
  studentIPage(state) {
    return state.studentIPage || [];
  },
  studentList(state) {
    return state.studentIPage ? state.studentIPage.records || [] : [];
  },
  depAndMajorMenu(state) {
    return state.depAndMajorMenu || [];
  },
};
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters,
};
